package com.gsafety.wellsfort.common.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Data;

/**
 * API 返回类
 *
 * @author tangjiawei
 * @date 2019/1/12
 */
@Data
@ApiModel
public class ApiResponse<T> {
    /**
     * 状态码 0表示成功 1表示通用失败
     */
    @ApiModelProperty(value = "状态码 0表示成功 1表示通用失败")
    private Integer code;

    /**
     * 返回信息描述
     */
    @ApiModelProperty(value = "返回信息描述")
    private String msg;

    /**
     * 数据数量
     */
    @ApiModelProperty(value = "数据数量")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer count;

    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;


    private ApiResponse() {

    }


    public static ApiResponse success() {
        return success(ResultCode.SUCCESS.getMsg());
    }

    public static <T> ApiResponse success(T data) {
        return success(data, null, ResultCode.SUCCESS.getMsg());
    }

    public static ApiResponse success(String msg) {
        return success(null, null, msg);
    }

    public static <T> ApiResponse success(T data, String msg) {
        return success(data, null, msg);
    }

    public static <T> ApiResponse success(T data, Integer count) {
        return success(data, count, ResultCode.SUCCESS.getMsg());
    }

    public static <T> ApiResponse success(T data, Integer count, String msg) {
        ApiResponse response = new ApiResponse();
        response.setData(data);
        response.setCount(count);
        response.setCode(ResultCode.SUCCESS.getCode());
        response.setMsg(msg);
        return response;
    }

    public static ApiResponse success(ResultCode result) {
        ApiResponse response = new ApiResponse();
        response.setCode(ResultCode.SUCCESS.getCode());
        response.setMsg(result.getMsg());
        return response;
    }


    public static ApiResponse error() {
        return error(ResultCode.ERROR);
    }

    public static ApiResponse error(ResultCode result) {
        ApiResponse response = new ApiResponse();
        response.setCode(result.getCode());
        response.setMsg(result.getMsg());
        return response;
    }

    public static <T> ApiResponse error(ResultCode result, T data) {
        ApiResponse response = new ApiResponse();
        response.setCode(result.getCode());
        response.setMsg(result.getMsg());
        response.setData(data);
        return response;
    }

    public static ApiResponse error(int code, String msg) {
        ApiResponse response = new ApiResponse();
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

}
